# CourseHub Microservices System Test Script
Write-Host "Testing CourseHub Microservices System" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

function Test-Service {
    param(
        [string]$ServiceName,
        [string]$Url
    )
    
    Write-Host "`nTesting $ServiceName..." -ForegroundColor Yellow
    try {
        $response = Invoke-WebRequest -Uri $Url -Method GET -TimeoutSec 10
        if ($response.StatusCode -eq 200) {
            Write-Host "✓ $ServiceName is healthy" -ForegroundColor Green
        } else {
            Write-Host "✗ $ServiceName returned status: $($response.StatusCode)" -ForegroundColor Red
        }
    } catch {
        Write-Host "✗ $ServiceName is not accessible: $($_.Exception.Message)" -ForegroundColor Red
    }
}

# Test all services
Test-Service "Consul" "http://localhost:8500/v1/status/leader"
Test-Service "API Gateway" "http://localhost:8080/actuator/health"
Test-Service "User Management Service" "http://localhost:8081/actuator/health"
Test-Service "Course Management Service" "http://localhost:8082/actuator/health"
Test-Service "Exercise Management Service" "http://localhost:8083/actuator/health"
Test-Service "Learning Progress Service" "http://localhost:8084/actuator/health"

# Test user registration
Write-Host "`nTesting User Registration via API Gateway..." -ForegroundColor Yellow
try {
    $body = @{
        username = "testuser"
        email = "test@example.com"
        password = "password123"
    } | ConvertTo-Json
    
    $response = Invoke-WebRequest -Uri "http://localhost:8080/api/users/register" -Method POST -ContentType "application/json" -Body $body -TimeoutSec 10
    Write-Host "✓ User registration test completed with status: $($response.StatusCode)" -ForegroundColor Green
} catch {
    Write-Host "✗ User registration test failed: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n========================================" -ForegroundColor Green
Write-Host "System Test Complete" -ForegroundColor Green
Read-Host "Press Enter to continue"